package readFromDB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import classes.Students;
import classes.DB;
import classes.Students;

public class GetStudent {
	Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql="";
    List<Students> studentList;
   // 读取全部课程
	public List<Students> getByTableName(String tablename) throws SQLException {
		try { 
			studentList=new ArrayList<Students>();
			conn=DB.GetConnection();
			stmt =conn.createStatement();
			sql="select * from "+tablename;
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Students tempStudents=new Students();
				tempStudents.setStuId(rs.getString("Sno"));
				tempStudents.setStuName(rs.getString("Sname"));
				tempStudents.setStuGrage(rs.getString("Sgrade"));
				tempStudents.setStuProfess(rs.getString("Sdept"));
				tempStudents.setStuInstitute(rs.getString("Saca"));
				tempStudents.setStuPwd(rs.getString("Spswd"));
				tempStudents.setStuPhhone(rs.getString("Sphone"));	
				tempStudents.setStuCourselearned(rs.getString("sclno"));
				studentList.add(tempStudents);
			}
	         rs.close();
	         stmt.close();
	         conn.close();
	     } 
	    
	     catch (SQLException e) {
	         e.printStackTrace();
	         System.out.println("无法连接数据库");
	     }
	     catch(Exception e){
	         // 处理 Class.forName 错误
	         e.printStackTrace();
	     }
		return studentList;
		}
	}
	
